<?php
	/**
	* @File name:	router.php
	* @Desription:	router
	* @Author:	 	jinxin<jx@178.com>   
	* @Date:	 	Wed Jun 08 11:17:20 CST 2011
	* @Copyright 	Copytright (c) 2008-2011 178.COM Co., Ltd.
	* @version	 	$Id $
	*/
	
	
	class router{
		static $getInstence;
		static $class =  __CLASS__;
		public $app;
		public $controller;
		public $action;
		public $site;
		
		public function __construct(){}
		public function __destruct(){}
		
		public static function getInstence(){
			if (!self::$getInstence){
				self::$getInstence = new self::$class();
			}
			return self::$getInstence;
		}
		
		/**
		 * initialize router
		 *
		 */
		public function initialize(){
			if ($this->parseURI()){
				$this->route($_REQUEST);
			}
		}
		
		/**
		 * format url
		 *
		 * @return array
		 */
		public function parseURI(){
			$url = substr($_SERVER['REQUEST_URI'],0,4) == 'http' ? $_SERVER['REQUEST_URI'] : 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
			$urlInfo = parse_url($url);
			try {
				if (!in_array($_SERVER['HTTP_HOST'],$_SERVER['config']['phpsite'])){
					throw  new Exception('wrong_hosts');
				}
				$this->site = $_SERVER['config']['php_path_subfix'][$_SERVER['HTTP_HOST']];
				//print_r($_SERVER);exit;
				$pathInfo = substr($_SERVER['SERVER_SOFTWARE'],0,5) == 'nginx' ? $urlInfo['path'] : (isset($_SERVER['PATH_INFO']) ?  $_SERVER['PATH_INFO'] : '');
				if ($pathInfo != '') {
					$qath = substr($pathInfo,1);
					$params = explode('/',$qath);
					$_REQUEST['app'] = $_GET['app']  = isset($params[0]) && $params[0] != '' ? $params[0] : 'index';
					$_REQUEST['controller'] = $_GET['controller']  = isset($params[1]) && $params[1] != '' ? $params[1] : 'index';
					$_REQUEST['action'] = $_GET['action']  = isset($params[2]) && $params[2] != '' ? $params[2] : 'index';
					$count = count($params);
					if ($count > 3) {
						$uri = array();
						foreach ($params as $key=>$value){
							if ($key >= 3 && $key % 2 == 1 && $value!=''){
								$qv = intval($key +1) > ($count -1) || $params[intval($key+1)]=='' ? '' : $params[intval($key+1)];
								$_REQUEST[$value] = $_GET[$value] = $qv;
							}
						}
					}
				}
				return $_REQUEST;
			}catch (Exception $e){
				$error = $e->getMessage();
				error::show($error);
			}
		}
		
		public function getController(){
			$controllerName = $this->site.$this->app.'_controller_'.$this->controller;
			if (!class_exists($controllerName)){
				$msg = $_SERVER['config']['debug'] ? 'broken_params' : 'no_uri';
				echo $controllerName;
				throw new Exception($msg);
			}else {
				$controller =  new $controllerName($this->app,$this->controller,$this->site);
				if (!method_exists($controller,$this->action)){
					$msg = $_SERVER['config']['debug'] ? 'no_page' : 'no_uri';
					throw new Exception($msg);
				}
				return $controller;
			}
		}
		
		public function route($params)
		{
			$this->app = isset($params['app']) ? $params['app'] : 'index';
			$this->controller = isset($params['controller']) ? $params['controller'] : 'index';
			$this->action = isset($params['action']) ? $params['action'] : 'index';
			try {
				$controller = $this->getController();
				$controller->launch($this->action);
			}catch (Exception $e){
				$error = $e->getMessage();
				error::show($error);
			}
		}
	}
	
